﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace PaymentService
{

    public class PaymentService : IPaymentService
    {
        AccountBalanceEntities context = new AccountBalanceEntities();

        public bool GetPaymentStatus(PaymentDetails payment)
        {
            try
            {
                var pmt = (from p in context.Payments
                           where p.CreditCardNo == payment.CreditCardNo
                           select p).FirstOrDefault();
                if (pmt == null)
                    throw new Exception("Invalid Credit Card No");

                if (pmt.ExpiryDate != payment.ExpiryDate)
                    throw new Exception("Credit card details not matching");

                if (pmt.Name != payment.PayerName)
                    throw new Exception("Credit card details not matching");

                if (pmt.AccountBalance < payment.PaymentAmount)
                    throw new Exception("Insufficient Balance");

                else
                    return true;
            }
            catch (Exception e)
            {
                PaymentIssue pi = new PaymentIssue();
                pi.CreditCardNo = payment.CreditCardNo + " ";
                pi.Issue = e.Message;
                
                throw new FaultException<PaymentIssue>(pi, new FaultReason("Error in Payment"));
            }
        }
    }
}


